package com.cwz.logo;

import java.util.HashMap;
import java.util.Map;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class SAXHandler extends DefaultHandler {

	private String current;
	private Map<String, String> map = new HashMap<String, String>();
	private String currentTitle;

	public void startDocument() throws SAXException {
		super.startDocument();
	}

	public void endDocument() throws SAXException {
		super.endDocument();
		DataProvider.setMap(map);
	}

	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		super.startElement(uri, localName, qName, attributes);
		current = localName;
	}

	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		super.endElement(uri, localName, qName);
		current = "";
	}

	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if (current.equalsIgnoreCase("title")) {
			String title = new String(ch, start, length);
			currentTitle = title;
			// System.out.print(title + ": ");
		} else if (current.equalsIgnoreCase("description")) {
			String data = new String(ch, start, length);
			int a = data.indexOf("img src=");
			if (a != -1) {
				String result = data.substring(a, data.length());
				String[] strs = result.split("\"");
				String logoUrl = strs[1];
				map.put(currentTitle, logoUrl);
				// System.out.println(logoUrl);
			}
		}

	}
}
